package actions;

import java.io.File;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@InterceptorRefs({
	@InterceptorRef(value = "fileUpload", params = { "allowedTypes",
			"application/pdf" }),
	@InterceptorRef(value = "defaultStack") })


@Action(value = "/UploadPDF")
@Results({
	@Result(location = "/jsp/principal/backend.jsp", name = "input"),
	@Result(location = "/jsp/principal/backend.jsp", name = "SUCCESS") })

public class UploadPDFAction extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String filePDF; 
	private String NombrePDF; 
	private Map<String, Object> session;
	
	public String execute() {		
		session=ActionContext.getContext().getSession();
		session.put("contenido", "/jsp/contenidoBack/listarTesis.jsp");
		try {
			String filePath = ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/");       
			 String FinalArchivo=filePath.concat(getNombrePDF());
			File fileToCreate = new File(FinalArchivo);  
			File file = new File(filePDF);
			FileUtils.copyFile(file, fileToCreate); 
		} catch (Exception e) {
			e.printStackTrace();
			addActionError(e.getMessage());
		}

		return "SUCCESS";
	}

	
	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		
	}


	public String getFilePDF() {
		return filePDF;
	}


	public void setFilePDF(String filePDF) {
		this.filePDF = filePDF;
	}


	public String getNombrePDF() {
		return NombrePDF;
	}


	public void setNombrePDF(String nombrePDF) {
		NombrePDF = nombrePDF;
	}

}

